package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.ShippingInfo;
import com.tyqx.hashchainbiz.service.ShippingProductService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.ShippingInfoMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("shippingProductService")
public class ShippingProductServiceImpl implements ShippingProductService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private ShippingInfoMapper shippingInfoMapper;

    @Async
    public Integer shippingProduct(ShippingInfo shippingInfo) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        shippingInfo.setCreateTime(createTime);
        shippingInfo.setUpdateTime(createTime);
        String shippingInfoStr= shippingInfo.toString();
        HashIndex hashIndex= HashValueUtil.getHashValue(shippingInfoStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        shippingInfo.setHashValue(hashIndex.getHashValue());

        return shippingInfoMapper.addShippingInfo(shippingInfo);
    }

    @Override
    public List<ShippingInfo> getShippingProductById(String donateId, String userName) {
        return shippingInfoMapper.getShippingProductById(donateId);
    }

    @Override
    public List<ShippingInfo> getShippingProductByTime(Date startTime, Date endTime, String userName) {
        return shippingInfoMapper.getShippingProductByTime(startTime, endTime, userName);
    }
}
